package com.dongdong.bookstore.module.ams.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ams.model.Article;
import com.dongdong.bookstore.module.ams.mapper.ArticleMapper;
import com.dongdong.bookstore.module.ams.service.AmsCacheService;
import com.dongdong.bookstore.module.ams.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongdong.bookstore.module.ams.viewmodel.ArticleAddParam;
import com.dongdong.bookstore.module.cms.mapper.UserMapper;
import com.dongdong.bookstore.module.cms.model.User;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 文章表  服务实现类
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-22
 */
@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AmsCacheService amsCacheService;

    @Override
    public boolean judgeLike(Long articleId, Long userId) {
        Article article = new Article();
        User user = new User();

        article = articleMapper.selectById(articleId);
        Asserts.isNull(article,"没有该文章，不能点赞");
        user = userMapper.selectById(userId);
        Asserts.isNull(user,"用户不存在，不能点赞");

        // true 是点赞成功 false 是取消点赞
        return amsCacheService.judgetLike(articleId, userId);
    }



    @Override
    public Integer addArticle(ArticleAddParam param) {
        Article article = new Article();
        BeanUtils.copyProperties(param,article);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Article::getAdminId,param.getAdminId());


        int insert = articleMapper.insert(article);

        if(insert > 0){
            return insert;
        }
        Asserts.fail("添加文章失败");
        return 0;
    }

    @Override
    public boolean deleteArticle(Long id) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Article::getId,id);
        Asserts.isFalse(remove(wrapper),"删除文章失败");
        // 删除文章 点赞量 和 点击量缓存
        amsCacheService.delLike(id);
        amsCacheService.delViews(id);
        return true;
    }

    @Override
    public boolean updateArticle(Long id, ArticleAddParam articleAddParam) {
        Article article1 = articleMapper.selectById(id);

        BeanUtils.copyProperties(articleAddParam,article1);
        System.out.println(article1);
        int i = baseMapper.updateById(article1);
        if(i>0){
            return true;
        }
        Asserts.fail("更新文章失败");
        return false;
    }

    /**
     *
     * @return '返回数组  一页显示10个数据'
     */
    @Override
    public Page<Article> getArticleListByPage(String keyword, Integer pageSize, Integer pageNum) {
        //初识化分页
        Page<Article> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Article> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(keyword)){
            //根据文章标题查找文章
            lambda.like(Article::getTitle,keyword);
            //根据管理员查找  管理员发布的文章
            lambda.or().like(Article::getAdminId,keyword);

        }
        lambda.orderByDesc(Article::getId);
        return page(page,wrapper);
    }

    @Override
    public Article getArticleById(long id) {
        if(id > 0){
            Article article = articleMapper.selectById(id);
            Asserts.isNull(article,"无此文章或已被管理员删除");
            //添加文章点击量
            amsCacheService.addView(id);
            return article;
        }
        Asserts.fail("查询文章详情需要ID");
        return null;
    }
}
